package com.tf.integrated.controller;

import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.config.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
 * 配置管理接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "配置管理接口",description="ConfigController")
@RestController
@RequestMapping("/config")
public class ConfigController extends BaseController
{
    @ApiOperation("获取当前配置")
    @GetMapping("/")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String select(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            String query = Config.buildMap(data);
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            logger.error("select on  config/ query===" + query);
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/?" + query, method, null, headers);
            logger.error("select on  config/ analyseResults===" + analyseResults);
            LogController.add("/config/",LogController.METHOD_GET,1);
            return analyseResults;
        }
        LogController.add("/config/",LogController.METHOD_GET,0);
        return "{}";
    }

    @ApiOperation("设置当前配置")
    @PutMapping("/")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String putset(@RequestParam Map<String, String> data,@RequestBody String binfo)
    {
        if(data!=null&&data.size()>0&&binfo!=null)
        {
            String host = (String)data.get("host");
            String query = Config.buildMap(data);
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            logger.error("putset on  config/ query===" + query);
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/?" + query, method, binfo, headers);
            logger.error("putset on  config/ analyseResults===" + analyseResults);
            LogController.add("/config/",LogController.METHOD_PUT,1);
            return analyseResults;
        }
        LogController.add("/config/",LogController.METHOD_PUT,0);
        return "{}";
    }

    @ApiOperation("导出配置文件")
    @GetMapping("/cfgFile")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String cfgFile(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/cfgFile", method, null, headers);
            logger.error("cfgFile on  config/cfgFile analyseResults===" + analyseResults);
            LogController.add("/config/cfgFile", LogController.METHOD_GET, 1);
            return analyseResults;
        }
        LogController.add("/config/cfgFile", LogController.METHOD_GET, 0);
        return "{}";
    }

    @ApiOperation("导入配置文件")
    @PostMapping("/cfgFile")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String cfgFileIn(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.POST;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/cfgFile", method, null, headers);
            logger.error("cfgFileIn on  config/cfgFile analyseResults===" + analyseResults);
            LogController.add("/config/cfgFile", LogController.METHOD_POST, 1);
            return analyseResults;
        }
        LogController.add("/config/cfgFile", LogController.METHOD_POST, 0);
        return "{}";
    }

    @ApiOperation("导出配置文件到U盘")
    @GetMapping("/cfgFile/to")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String cfgFileto(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            String query = Config.buildMap(data);
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            logger.error("cfgFileto on  config/cfgFile/to query===" + query);
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/cfgFile/to?" + query, method, null, headers);
            logger.error("cfgFileto on  config/cfgFile/to analyseResults===" + analyseResults);
            LogController.add("/config/cfgFile/to",LogController.METHOD_GET,1);
            return analyseResults;
        }
        LogController.add("/config/cfgFile/to",LogController.METHOD_GET,0);
        return "{}";
    }

    @ApiOperation("获取默认配置")
    @GetMapping("/default")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String getdefault(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            String query = Config.buildMap(data);
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            logger.error("getdefault on  config/default query===" + query);
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/config/default?" + query, method, null, headers);
            logger.error("getdefault on  config/default analyseResults===" + analyseResults);
            LogController.add("/config/default",LogController.METHOD_GET,1);
            return analyseResults;
        }
        LogController.add("/config/default",LogController.METHOD_GET,0);
        return "{}";
    }
}